-- star_damage
-- create by xinj
-- 技能: 施放N颗流星
-- 目前支持如下两种配置：
-- 1、star_damage(600,5)：施放5颗流星，每颗600威力的魔法伤害
-- 2、star_damage(600,5,style,2000)：施放5颗流星，每颗600威力的魔法伤害，如果目标是style系，则效果增强200%


return {
    apply = function(source, target, skillId, round, para, extra)
        -- 威力，半径
        local base = para[1];
        local num = para[2];

        local combine = PropM.combine;
        local PropM_apply = PropM.apply;
        local fetchProps = PropM.fetchProps;
        local trigger = PropM.trigger;

        -- 计算魔法效果加成
        local addon_rate = SkillM.calcMagicEffectAddon(source, skillId, true);

        -- 最终威力
        local force = math.modf(base * (addon_rate + 1000)/1000);

        -- 2. 攻击方的魔力
        local magic = source:getMagic();

        -- 被对方百分比削弱
        local prop = combine(target, "weak_enemy", "magic");
        magic = PropM_apply(prop, magic);

        -- 被对方绝对值削弱
        prop = combine(target, "weak_enemy2", "magic");
        magic = PropM_apply(prop, magic);

        -- 3. 伤害/计算时魔力+3
        local damage = math.modf(force * (magic + 3) / 1000);

        -- 数量增加
        local props = fetchProps(source, "skill_goal_add");
        for _, prop in ipairs(props) do
            if tonumber(prop[2]) == skillId then
                num = PropM_apply(prop, num);
            end
        end

        -- 单个流星伤害，每次伤害需要单独计算怪物系别和抗性
        local singleDamage;

        -- 连续打击目标
        local i;
        local pos;
        local grid;
        for i = 1, num do
            if target:isDead() then
                for pos = 1, DUNGEON_WIDTH * DUNGEON_HEIGHT do
                    grid = DungeonM.getGridByPos(pos);

                    -- 翻开且存活的怪物
                    if  grid:isOpened() and
                        grid:isMonster() and
                        not grid.monster:isDead() then
                        target = grid.monster;
                        break;
                    end
                end

                -- 没选到，还是原来那头
                if target:isDead() then
                    return { target, };
                end
            end

            singleDamage = damage;
            if #para == 4 and tonumber(para[3]) == target.style then
                -- 该系别有伤害加成,需重新计算伤害
                singleDamage = math.modf((force + base * para[4] / 1000) * (magic + 3) / 1000);
            end

            -- 魔法暴击类，多种暴击线性叠加伤害
            local addon_damage = 0;

            -- 伤害类魔法概率增强
            props = fetchProps(source, "damage_magic");
            for _, prop in ipairs(props) do
                addon_damage = addon_damage + trigger(source, prop[1], prop[2], singleDamage);
            end
            -- 魔法暴击
            props = fetchProps(source, "critical_magic");
            for _, prop in ipairs(props) do
                addon_damage = addon_damage + trigger(source, prop[1], prop[2], singleDamage);
            end
            -- 概率双倍
            props = fetchProps(source, "skill_double");
            for _, prop in ipairs(props) do
                if tonumber(prop[2]) == skillId then
                    addon_damage = addon_damage + trigger(source, prop[1], prop[2], singleDamage);
                end
            end
            -- 最终伤害
            singleDamage = singleDamage + addon_damage;

            -- 魔法抗性
            prop = combine(target, "resist", "magic");
            -- 忽视抗性
            local p = combine(source, "ignore_resist", skillId);
            prop[3] = PropM_apply(p, prop[3]);
            p = combine(source, "ignore_resist", "magic");
            prop[3] = PropM_apply(p, prop[3]);

            singleDamage = PropM_apply(prop, singleDamage);

            -- 增加命中动作
            if SkillM.getSequence(target) == nil then
                SkillM.getSequenceList()[target] = CastSequence.create(source, monster, skillId);
            end

            SkillM.getSequence(target):hit(source, target, skillId);
            -- 受创
            CombatM.receiveDamage(source, target, singleDamage, skillId, extra);
        end

        return { target, };
    end,

    parse = function(desc)
        local arr = string.explode(desc, ",");

        if #arr == 2 then
            -- 规则1：star_damage(威力,数量)
            return { tonumber(arr[1]), tonumber(arr[2]) };
        else
            -- 规则2：area_damage(威力,数量,style,2000)
            return { tonumber(arr[1]), tonumber(arr[2]), tonumber(arr[3]), tonumber(arr[4]), };
        end
    end,

    -- 解析技能描述
    parseDesc = function(source, skillId, desc, para)
        local base = para[1];
        local num   = para[2];

        -- 计算魔法效果加成
        local addon_rate = SkillM.calcMagicEffectAddon(source, skillId, false);

        -- 最终威力
        local force = math.modf(base * (addon_rate + 1000)/1000);

        -- 显示红龙天赋数量增加
        props = PropM.fetchProps(source, "skill_goal_add");
        for _, prop in pairs(props) do
            if tonumber(prop[2]) == skillId then
                num = PropM.apply(prop, num);
            end
        end

        -- 2. 攻击方的魔力
        local magic = source:getMagic();

        -- 3. 伤害
        local damageBase = math.modf(base * (magic + 3) / 1000);
        local damageForce = math.modf(force * (magic + 3) / 1000);
        local damageAddon = damageForce - damageBase;
        if damageAddon == 0 then
            desc = string.gsub(desc, "{star_damage}", damageForce);
        else
            desc = string.gsub(desc, "{star_damage}", damageBase .. "(+" .. damageAddon .. ")");
        end

        desc = string.gsub(desc, "{num}", num);

        -- 系别TODO

        return desc;
    end,
}
